package com.gete.schdule;

import com.sun.mail.util.MailSSLSocketFactory;
import io.gate.gateapi.ApiClient;
import io.gate.gateapi.api.SpotApi;
import io.gate.gateapi.models.Order;
import io.gate.gateapi.models.OrderBook;
import io.gate.gateapi.models.Trade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;

import static io.gate.gateapi.models.Order.FinishAsEnum.FILLED;

@Component
@Slf4j
public class CountExchange {
    private static final String API_KEY = "b71a539ad55d7a21f2e25fe9e8803cc9";
    private static final String SECRET_KEY = "2a1f1178e442b556514e9a502004ff5cdcceb596fdb2c81eb6155d78415d354f";

    @Scheduled(fixedRate = 3000) // 每天凌晨8点执行
    public void yourScheduledMethod() {
        /*StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try {
            String currencyPair = "SATS_USDT";
            // 执行你的定时任务逻辑
            log.info("每天的定时任务执行统计前一天8点到此刻的订单成交数量");
            ApiClient client = new ApiClient();
            client.setApiKeySecret(API_KEY, SECRET_KEY);
            // Setting basePath is optional. It defaults to https://api.gateio.ws/api/v4
            client.setBasePath("https://api.gateio.ws/api/v4");
            SpotApi spotApi = new SpotApi(client);
            List<Trade> execute = spotApi.listTrades(currencyPair).limit(1000).execute();
            List<String> collect = execute.stream().map(k -> k.getCreateTime()).collect(Collectors.toList());
            log.info("交易信息{}");
        } catch (Exception exception) {
            log.info("读取异常{}",exception);

        } finally {
            stopWatch.stop();
            log.info("统计订单发送统计耗时{}", stopWatch.getTotalTimeMillis());
        }*/

    }

    private static void testMail() throws Exception {
        // 给用户发送邮件的邮箱
        final String from = "2191759543@qq.com";
        // 邮箱的用户名
        final String username = "2191759543@qq.com";
        // 邮箱授权码，刚刚保存的授权码，不是qq密码
        final String password = "srgxwfnmeiemecah";
        // 发送邮件的服务器地址，QQ服务器
        final String host = "smtp.qq.com";
        // 接收人邮箱
        final String to = "821512040@qq.com";
        // 邮件主题
        final String title = "exchangeCountInfo";

        // 使用QQ邮箱时配置
        Properties prop = new Properties();
        prop.setProperty("mail.host", "smtp.qq.com");    // 设置QQ邮件服务器
        prop.setProperty("mail.transport.protocol", "smtp");      // 邮件发送协议
        prop.setProperty("mail.smtp.auth", "true");      // 需要验证用户名和密码
        // 关于QQ邮箱，还要设置SSL加密，其他邮箱不需要
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);

        // 创建定义整个邮件程序所需的环境信息的 Session 对象，QQ才有，其他邮箱就不用了
        Session session = Session.getDefaultInstance(prop, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 发件人邮箱用户名，授权码
                return new PasswordAuthentication(username, password);
            }
        });

        // 开启 Session 的 debug 模式，这样就可以查看程序发送 Email 的运行状态
        session.setDebug(true);

        // 通过 session 得到 transport 对象
        Transport ts = session.getTransport();

        // 使用邮箱的用户名和授权码连上邮箱服务器
        ts.connect(host, username, password);

        // 创建邮件，写邮件
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from)); // 指明邮件的发件人
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));   // 指明邮件的收件人
        message.setSubject(title);     // 邮件主题
        message.setContent("验证码为:8824", "text/html;charset=utf-8");    // 邮件内容
        // 发送邮件
        ts.sendMessage(message, message.getAllRecipients());
        System.out.println("验证码发送成功");
        // 释放资源
        ts.close();


    }

    public static void main(String[] args) throws Exception {
        testMail();
    }

}

